<template>
    <div>
        <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
            <el-breadcrumb-item :to="{ path: '/sys-admin' }">
                <i class="el-icon-s-promotion"></i> 后台管理
            </el-breadcrumb-item>
            <el-breadcrumb-item>管理员列表</el-breadcrumb-item>
        </el-breadcrumb>

        <el-table :data="tableData" stripe style="width: 100%" border>
            <el-table-column prop="id" label="序号" align="center" width="40"></el-table-column>
            <el-table-column prop="username" label="姓名" align="center" width="120"></el-table-column>
            <el-table-column prop="nickname"  align="center" label="昵称" width="100"></el-table-column>
            <el-table-column prop="phone" align="center" label="手机号" width="110"></el-table-column>
            <el-table-column prop="email" align="center" label="邮箱"  width="180"></el-table-column>
            <el-table-column prop="description" header-align="center" label="描述"></el-table-column>
            <el-table-column prop="enable" align="center" label="是否启用" width="80">
                <template slot-scope="scope">
                    <el-switch
                            v-model="scope.row.enable"
                            :active-value="1"
                            :inactive-value="0"
                            active-color="#13ce66"
                            inactive-color="#ff4949"
                            @change="changeEnable(scope.row)">
                    </el-switch>
                </template>
            </el-table-column>
            <el-table-column align="center" label="操作" width="100">
                <template slot-scope="scope">
                    <el-button type="primary" icon="el-icon-edit" size="mini" circle @click="handleEdit(scope.row)"></el-button>
                    <el-button type="danger" icon="el-icon-delete" size="mini" circle @click="handleDelete(scope.row)"></el-button>
                </template>
            </el-table-column>
        </el-table>
    </div>

</template>

<script>
    export default {
        data() {
            return {
                tableData: [{
                    "id": 1,
                    "username": "root",
                    "nickname": "系统管理员",
                    "phone": "13900139001",
                    "email": "root@baidu.com",
                    "description": "最高权限的管理员",
                    "enable": 1
                }]
            }
        },
        methods:{
            loadAdminList(){
                let url = "http://localhost:9081/admin/list";
                console.log('loadAdminList()');
                this.axios
                        .create({
                            "headers":{
                                "Authorization": localStorage.getItem("jwt")
                            }
                        })
                        .get(url).then((response)=>{
                    this.tableData = response.data.data;
                })
            },
            changeEnable(admin){
                let enableText = ['禁用', '启用'];
                let url = 'http://localhost:9081/admin/' + admin.id;
                if (admin.enable == 1) {
                    url += '/enable';
                } else {
                    url += '/disable';
                }
                console.log('url = ' + url);
                this.axios.post(url).then((response) => {
                    let responseBody = response.data;
                    console.log(responseBody);
                    if (responseBody.state == 20000) {
                        this.$message({
                            message: '将【' + admin.username + '】的启用状态设置为【' + enableText[admin.enable] + '】成功',
                            type: 'success'
                        });
                    } else {
                        // this.$message.error(responseBody.message);
                        let title = '操作失败';
                        this.$alert(responseBody.message, title, {
                            confirmButtonText: '确定',
                            callback: action => {
                                this.loadAdminList();
                            }
                        });
                    }
                });
            },
            handleDelete(admin){
                this.$confirm('此操作将永久删除'+admin.username+', 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    let url = "http://localhost:9081/admin/deleteById?id="+admin.id;
                    this.axios.get(url).then((response)=>{
                        let responseBody = response.data;
                        if(responseBody.state!=20000){
                            this.$message.error(responseBody.message);
                        }
                        this.loadAdminList();
                    })
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });

            },
            handleEdit(admin){
                let url = "http://localhost:9081/admin/deleteById?id="+admin.id;
            }
        },
        created(){
            console.log('created()');
        },
        mounted(){
            console.log('mounted()');
            this.loadAdminList();
        }
    }
</script>

<style scoped>

</style>